package com.wg.msuser.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wg.msuser.entity.BrowseRecord;
import com.wg.msuser.service.BrowseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;


@RestController
@RequestMapping("/user/record")
public class BrowseRecordController {
    @Autowired
    private BrowseRecordService browseRecordService;

    @PostMapping ("/add")
    public boolean addBrowseRecord(@RequestBody BrowseRecord browseRecord){
        QueryWrapper<BrowseRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", browseRecord.getUserId()).eq("shopId", browseRecord.getShopId());

        BrowseRecord browseRecord1 = browseRecordService.getOne(queryWrapper);
        if (browseRecord1 == null){
            browseRecord.setTime(new Timestamp(System.currentTimeMillis()));
            return browseRecordService.save(browseRecord);
        }
        else{
            return false;
        }
    }
    public boolean updateBrowseRecord(BrowseRecord browseRecord){
        return browseRecordService.updateById(browseRecord);
    }
    public boolean deleteBrowseRecord(Integer id){
        return browseRecordService.removeById(id);
    }
    public BrowseRecord getBrowseRecord(Integer id){
        return browseRecordService.getById(id);
    }
}
